$(function () {
    layui.use(['jquery', 'table'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        //渲染表格
        table.render({
            elem: "#linkTable",//渲染的表格的id
            url: "/bbs/getFriendlyLink.action",//请求后台的接口
            cols: [[//表头
                {type: 'numbers', title: '编号', width: 120, align: 'center'},
                {field: 'link_id', title: 'id', width: 180, align: 'center'},
                {field: 'link_name', title: '链接名', width: 300, align: 'center'},
                {field: 'link_content', title: '地址', width: 340, align: 'center'},
                {title: '操作', toolbar: '#userOperation', width: 300, align: 'center'},
            ]],
            page: false//是否开启分页
        });

        //表格的重载
        function reloadLinkTable(){
            table.reload("linkTable",{
                where:{
                    link_id:$("link_id").val(),
                    link_name:$("link_name").val(),
                    link_content:$("link_content").val(),
                }
            },'data');
        }

        //监听表格的工具栏的事件
        table.on('tool(linkTable)',function (obj) {
            var data = obj.data;
            if(obj.event == 'updateFriendlyLink'){
                alert("编辑");
                //changeUserType(data.u_id);
            }else if(obj.event == 'deleteFriendlyLink') {
                alert("删除？");
                deleteFriendlyLink(data.link_id);
                //delUser(data.u_id);
            }
        });

        //编辑链接
        function deleteFriendlyLink(link_id){
            $.ajax({
                url:"/bbs/deleteFriendlyLink.action",
                type:"POST",
                data:{'link_id':link_id},
                dataType:'JSON',
                cache:false,
                success:function (data) {
                    if(data.code==200){
                        alert("删除成功!");
                        //重载表格
                        reloadLinkTable();
                    }else{
                        alert(data.msg);
                    }
                } ,
                error:function () {
                    alert("删除失败.")
                }
            });
        };

    });
})